package cz.acies.utils ;

import java.lang.reflect.*;

//******************************************************************************
//
//
//
//******************************************************************************

public class JavaUtils
{
	public static void main(String[] args) {
		try {
			System.out.println("JavaUtils.main()");
			/*
			String classname = "com.smart4web.erp.test.Config";
			Object[] argc = { new Integer(1) }; 
			Config obj = (Config)newObjectInstance(classname, argc);
			Object obj = newObjectInstance(classname, argc);
			
			System.out.println("obj.x = "+obj.getX());
			*/
			/*
			String classname = "com.smart4web.erp.core.Configurator";
			Object obj = (Workbench)createObjectInstance(classname);
			System.out.println("obj = "+obj);
			Object[] params = {"xxx"};
			String name = "print";
			System.out.println(executeMethod(obj, name, params));
			*/
			/*
			//String name = "getDocument";
			String name = "print";
			//Class[] parameterTypes = new Class[0];
			//Class[] 
			Class[] parameterTypes = { name.getClass() };
			Method method = obj.getClass().getMethod(name, parameterTypes);
			System.out.println("method = "+method);
			*/
			/*
			Object[] ar = { "xxx" };
			System.out.println(method.invoke(obj, ar)); 
			*/
			//method.invoke(obj, new Object[] { obj });
			//Document doc = null; 
			//Object[] args1 = {};
			//doc = (Document)method.invoke(doc, args1);
			/*
			String classname = "com.smart4web.schedule.SmartScheduleTimer";
			Object[] argc = { new Integer(1), "" };
			Object obj = newObjectInstance(classname, argc);
			//Object obj = createObjectInstance(classname);
			System.out.println("obj = "+obj);
			//ScheduleTimer obj = 
			*/
			/*
			String classname = "com.smart4web.erp.core.Configurator";
			Object obj = (Workbench)createObjectInstance(classname);
			System.out.println("obj = "+obj);
			Object[] params = {"xxx"};
			String name = "print";
			System.out.println(executeMethod(obj, name, params));
			*/
			//String name = "getDocument";
			//String name = "print";
			//Class[] parameterTypes = new Class[0];
			//Class[]
			/*
			Class[] parameterTypes = { name.getClass() };
			Method method = obj.getClass().getMethod(name, parameterTypes);
			System.out.println("method = "+method);
			
			
			Object[] ar = { "xxx" };
			System.out.println(method.invoke(obj, ar)); 
			*/
			//method.invoke(obj, new Object[] { obj });
			//Document doc = null; 
			//Object[] args1 = {};
			//doc = (Document)method.invoke(doc, args1);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	//private static JavaUtils util = new JavaUtils();
		
	//**************************************************************************
	//	Java Object Methods
	//**************************************************************************
	
	public static Object newObjectInstance(String className, Object[] args) throws Exception {
		try {
			Class<?>[] argc = new Class[args.length];
			for (int i = 0; i < args.length; i++) {
				argc[i] = args[i].getClass();
				//System.out.println("JavaUtils.argc["+i+"] = "+argc[i]+", args["+i+"]:"+args[i]);
			}
			Constructor<?> construct = Class.forName(className).getConstructor(argc);
			Object object = construct.newInstance(args);
			return object;
		} catch(Exception ex) {
			ex.printStackTrace();
			throw ex;
		}		
	}

	public static Object newObjectInstance(String className) throws Exception {
		Object object = null;
		try {
			object = Class.forName(className).newInstance();
		} catch(Exception ex) {
			 throw ex;
		}
		return object;
	}

	public static Object createObjectInstance(String className) throws Exception {
		Object object = null;
		try {
			object = Class.forName(className).newInstance();
		} catch(Exception ex) {
			 throw ex;
		}
		return object;
	}
	
	public static Object executeMethod(Object obj, String name, Object[] params) {
		try {
			Class<?>[] paramTypes = new Class[params.length];
			for (int i = 0; i < params.length; i++) {
				paramTypes[i] = params[i].getClass();
			}
			Method method = obj.getClass().getMethod(name, paramTypes);
			return method.invoke(obj, params);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
};